package com.wxy.web.controller;


import com.wxy.common.core.domain.AjaxResult;
import com.wxy.web.dto.MenuDto;
import com.wxy.web.model.Menu;
import com.wxy.web.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/admin/Menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @GetMapping("list")
    public AjaxResult list()
    {
        List<MenuDto> list = menuService.listAll();
        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG,list);
        return result;
    }


    @GetMapping("info")
    public AjaxResult info(Integer id)
    {
        Menu menu = menuService.getById(id);
        AjaxResult result = AjaxResult.success();
        result.put("data",menu);
        return result;
    }

    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody Menu menu)
    {
        //permission.setId(null);
        Boolean success = menuService.save(menu);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }

    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody Menu menu)
    {
        Boolean success = menuService.updateById(menu);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }

    @PostMapping("del/{id}")
    public AjaxResult del(@PathVariable Integer id)
    {
        Boolean success = menuService.removeById(id);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }
}
